<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||	
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws. 
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.). 
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations). 
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Account/classes/PhpFox_ServiceRate.class.php, $Author:  $ */



/** Service for working rate functions
 * 
 * @package module.account
 * @version $Id: PhpFox_ServiceRate.class.php 46 2007-12-14 23:12:21Z phpfoxsv $
 *   
 */
class PhpFox_Mod_Account_ServiceRate extends BaseService 
{
    
    /** Is rater alread rate this user
     * 
     * @param PhpFox_Mod_Account_ItemAccount  $oRater  rater user
     * @param PhpFox_Mod_Account_ItemAccount  $oUser   rated user
     * @return bool true - already rated, false - not rated yet
     */
    function isRated($oRater, $oUser)
    {
        $iCnt = $oRater->oDb->getField('SELECT COUNT(*) FROM '.App::getT('user_rating').
                                       '  WHERE rater = '.$oRater->getId().
                                       '    AND user = '.$oUser->getId());
        return ($iCnt>0);
    }
    
    /** Add rating for user
     * 
     * @param PhpFox_Mod_Account_ItemAccount  $oRater  rater user
     * @param PhpFox_Mod_Account_ItemAccount  $oUser   rated user
     * @param int                             $iRating rating
     * @return bool true - rate added, false - error occured
     */
    function addRate($oRater, $oUser, $iRating)
    {
        //delete old rating (if any was)
        $oRater->oDb->query('DELETE FROM '.App::getT('user_rating').
                            '  WHERE user = '.$oUser->aData[$oUser->sId].
                            '    AND rater = '.$oRater->aData[$oRater->sId]);
        $aVals = array(
            'rater'  => $oRater->aData[$oRater->sId],
            'user'   => $oUser->aData[$oUser->sId],
            'rating' => $iRating,
            'time'   => time(),
        );
        $oRater->oDb->insert(App::getT('user_rating'), $aVals);
        
        //update user rate
        $aRateInfo = $this->getRateInfo($oUser);
        $oUser->aData['user_rating'] = $aRateInfo['average'];
        $oUser->update(array('user_rating'));
                
        return true;
    }
    
    /** Get user rating info
     * 
     * @param PhpFox_Mod_Account_ItemAccount  $oUser  user
     * @return array rating info: 'total' => total rating, 'average' => average rating
     */
    function getRateInfo($oUser)
    {
        assert(null != $oUser);
        $aInfo = $oUser->oDb->getRow('SELECT COUNT(*) AS total, AVG(rating) AS average '.
                                     '  FROM '.App::getT('user_rating').
                                     '  WHERE user = "'.$oUser->getId().'"');
        return $aInfo;
    }
}
?>